const mongoose = require('mongoose')
const Schema = mongoose.Schema
const ObjectId = Schema.Types.ObjectId

const ThingSchema = new Schema({
    thingName: {
        type: String,
        required: true
    },
    place: {
        type: ObjectId,
        ref: "Thing"
    },      //每个thing只可能对应1个place
    picKey: String, //七牛云上的照片key，前期可以不填
    remark: {
        type: String,
        capped: 512 //限定最大512字节
    }, //我现在还不知道这么规定长度
    meta: {
        createdAt: {
            type: Date,
            default: Date.now()
        },
        updatedAt: {
            type: Date,
            default: Date.now()
        }
    }
});

ThingSchema.pre('save', function (next) {
    if( this.isNew ) {
        this.meta.createdAt = this.meta.updatedAt = Date.now()
    } else {
        this.meta.updatedAt = Date.now()
    }

    next()
})

mongoose.model('Thing', ThingSchema)